package uml.behavioral.state;

/**
 * 状态--具体状态类<br>
 * on 2023/1/6 0:16
 */
public class OverdraftState extends AccountState {
    private float ration = 0.01f;

    public OverdraftState(Account acc) {
        super(acc);
    }

    public OverdraftState(AccountState state) {
        super(state);
    }

    @Override
    public void withdraw(double amount) {
        double balance = acc.getBalance();
        if (balance - amount < -2000) {
            System.out.println("余额不足以供取款");
        } else {
            acc.setBalance(balance - amount);
            stateCheck();
        }
    }

    @Override
    public void deposit(double amount) {
        acc.setBalance(acc.getBalance() + amount);
        stateCheck();
    }

    @Override
    public void computeInterest() {
        System.out.println("账户需支付透支利息(1%):" + -acc.getBalance() * ration);
    }

    @Override
    public void stateCheck() {
        if (acc.getBalance() > 0) {
            acc.setState(new NormalState(this));
        } else if (acc.getBalance() == -2000) {
            acc.setState(new RestrictedState(this));
        } else if (acc.getBalance() < -2000) {
            System.out.println("操作受限");
        }
    }
}
